package com.ruoyi.business.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.domain.RuleIn;
import com.ruoyi.business.common.config.RiskruleConfig;
import com.ruoyi.common.constant.RiskConstants;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.Func;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Component
public class RestTemplateService {
    @Autowired
    RiskruleConfig riskruleConfig;
    private static RestTemplate restTemplate;

    static {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(300000);
        factory.setReadTimeout(300000);
        restTemplate = new RestTemplate(factory);
    }

    public int updateExpiryDate(CustomerInfo customerInfo) {
        log.info("远程调用updateExpiryDate方法,参数为:{}", customerInfo);
        int result = restTemplate.postForObject(riskruleConfig.getUpdateExpiryDate(), customerInfo, int.class);
        log.info("远程调用updateExpiryDate方法,结果返回:{}", result);
        return result;
    }

    public RuleIn initRuleInParam(String loanSerialNo, String customerNo) {
        log.info("远程调用initRuleInParam方法,参数为:{},{}", loanSerialNo, customerNo);
        RuleIn result = restTemplate.getForEntity(riskruleConfig.getInitRuleInParam().replace("LOANSERIALNO", loanSerialNo).replace("CUSTOMERNO", customerNo), RuleIn.class).getBody();
        log.info("远程调用initRuleInParam方法,结果返回:{}", result);

        if (Func.isEmpty(result)) {
            throw new BusinessException(RspEnmu.RISK_SERVICE_EXCEPTION);
        }
        return result;
    }

    public JSONObject riskRuleEngine(String applyType, String serialNo) {
        log.info("远程调用riskRuleEngine方法，参数为:{},{}", applyType, serialNo);
        JSONObject result = restTemplate.getForEntity(riskruleConfig.getRiskRuleEngine().replace("APPLYTYPE", applyType).replace("SERIALNO", serialNo), JSONObject.class).getBody();
        log.info("远程调用riskRuleEngine方法，结果返回:{}", result);

        this.afterConfirm(result);
        return result;
    }

    public JSONObject riskRuleRaiseLimits(String loanSerialNo, String customerNo, RuleIn ruleIn) {
        log.info("远程调用riskRuleRaiseLimit方法,参数为:{},{},{}", loanSerialNo, customerNo, ruleIn);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("loanSerialNo", loanSerialNo);
        jsonObject.put("customerNo", customerNo);
        jsonObject.put("ruleIn", ruleIn);
        JSONObject result = restTemplate.postForObject(riskruleConfig.getRiskRuleRaiseLimits(), jsonObject, JSONObject.class);
        log.info("远程调用riskRuleRaiseLimit方法,结果返回:{}", result);

        this.afterConfirm(result);
        return result;
    }

    public JSONObject riskRuleRaiseLimit(String ruleType, String customerNo) {
        log.info("远程调用riskRuleRaiseLimit方法，参数为:{},{}", ruleType, customerNo);
        JSONObject body = restTemplate.getForEntity(riskruleConfig.getRaiseLimit().replace("RULETYPE", ruleType).replace("CUSTOMERNO", customerNo), JSONObject.class).getBody();
        log.info("远程调用riskRuleRaiseLimit方法，结果返回:{}", body);

        this.afterConfirm(body);
        return body;
    }

    private void afterConfirm(JSONObject res) {
        String ruleResult = res.getString(RiskConstants.RULE_RESULT_DEAL_TYPE_KEY);
        if (Func.isEmpty(ruleResult)) {
            throw new BusinessException(RspEnmu.RISK_SERVICE_EXCEPTION);
        }
    }
}